const constant = require("./constant.js");
const storage = require("./storage.js");

function isPhone(phone) {
    if (!(/^1(3|4|5|6|7|8|9)\d{9}$/.test(phone))) {
        return false;
    }
    return true;
}

function showSuccessToast(config) {
    wx.showToast({
        title: config.title,
        icon: 'success',
        mask: true,
        duration: constant.duration,
        success: config.success
    });
}

function showFailToast(config) {
    wx.showToast({
        title: config.title,
        image: '/images/info.png',
        mask: true,
        duration: constant.duration,
        success: config.success
    });
}
function json2Form(json) {
  var str = [];
  for (var p in json) {
    str.push(encodeURIComponent(p) + "=" + encodeURIComponent(json[p]));
  }
  return str.join("&");
}
function formatTime(date) {
  var dat=new Date(date);
  var year = dat.getFullYear();
  var month = dat.getMonth()+1;
  var day = dat.getDate();
  var hou=dat.getHours();
  var min=dat.getMinutes();

  return year+"-"+month+"-"+day+" "+hou+":"+min;
}
function formatTimeToC(date) {
    var dat=new Date(date);
    var year = dat.getFullYear();
    var month = dat.getMonth()+1;
    var day = dat.getDate();
    var hou=dat.getHours();
    var min=dat.getMinutes();
  
    return year+"年"+month+"月"+day+"日"+hou+"时"+min+"分";
  }
function formatDate(date) {
    var dat=new Date(date);
    var year = dat.getFullYear();
    var month = dat.getMonth()+1;
    var day = dat.getDate();
  
    return year+"-"+month+"-"+day;
  }
  function formatDateToC(date) {
    var dat=new Date(date);
    var year = dat.getFullYear();
    var month = dat.getMonth()+1;
    var day = dat.getDate();
  
    return year+"年"+month+"月"+day+"日";
  }
function formatNumber(n) {
  n = n.toString()
  return n[1] ? n : '0' + n
}
function toThousands(num) {
  return (num || "").replace(/(\d)(?=(?:\d{3})+$)/g, '$1,');
}
module.exports = {
    isPhone: isPhone,
    showSuccessToast: showSuccessToast,
    showFailToast: showFailToast,
    json2Form:json2Form,
    formatTime: formatTime,
    formatDate: formatDate,
    formatTimeToC: formatTimeToC,
    formatDateToC: formatDateToC,
  toThousands: toThousands
};